package com.shujia.day19.mysql.pool;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MysqlConnection {
    // TODO Mysql连接类，在该类中，可以对MySQL连接进行初始化
    MysqlPoolConfig.MysqlConfig mysqlConfig;
    String mysqlConName;
    Connection connection;

    long createTime;
    long flagTime;  // 空闲的开始时间

    public MysqlConnection(MysqlPoolConfig.MysqlConfig mysqlConfig, String mysqlConName) {
        this.mysqlConName = mysqlConName;
        try {
            Class.forName(mysqlConfig.driver);
            connection = DriverManager.getConnection(mysqlConfig.url, mysqlConfig.user, mysqlConfig.passwd);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        createTime = System.currentTimeMillis();
        flagTime = createTime;
        System.out.println(mysqlConName+"连接已经创建完成..");
    }

    public Connection getConnection(){
        return this.connection;
    }

    public void close(){
        try {
            System.out.println(mysqlConName+"关闭当前连接");
            this.connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }



}
